﻿using System;
using System.Configuration;
using System.IO;
using System.Web;
using DFT.DB;
using DFT.Mail;

namespace DFT.AJAX
{
    public partial class ResetPasswordRequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String userEmail = Request.Form["email"];
            String newPassword = "";
            String fullName = "";
            String returnCdoe = "";

            Random rand = new Random();

            for (int i = 0; i < 8; i++)
            {
                switch (rand.Next(0, 3))
                {
                    case 0:
                        newPassword = newPassword + Convert.ToChar(rand.Next(48, 57));
                        break;
                    case 1:
                        newPassword = newPassword + Convert.ToChar(rand.Next(65, 90));
                        break;
                    case 2:
                        newPassword = newPassword + Convert.ToChar(rand.Next(97, 122));
                        break;
                }
            }
            if (DBManager.ResetPasswordRequest(userEmail, newPassword, ref fullName, ref returnCdoe))
            {
                if (returnCdoe == "0")
                {
                    if (Boolean.Parse(ConfigurationManager.AppSettings["MailEnabled"]))
                    {
                        String templatePath = HttpContext.Current.Server.MapPath("~//EmailTemplates//ResetPassword.htm");
                        String body = File.ReadAllText(templatePath);
                        body = body.Replace("@Name", fullName);
                        body = body.Replace("@Email", userEmail);
                        body = body.Replace("@Password", newPassword);
                        MailHelper.SendMailMessage(userEmail, "sub", body);
                    }
                }
            }
            Response.Write(returnCdoe);
        }
    }
}